﻿/// <reference path="../../Scripts/commen_js/MJ_JSComment.js" />
/// <reference path="../../Scripts/commen_js/jquery.cookie.min.js" />
/// <reference path="../../Scripts/commen_js/jquery-2.0.2.min.js" />
var BuyObject = {
    BindBata: function () {
        var productuid = GetQueryString("productid");
        var resoretype = GetQueryString("resoretype");
        var openid = $.cookie("openid");
        if (isUndefinedNullOrEmpty(openid)) {
            alert("下单失败请稍候再购买");
            return false;
        }
        poststr = "{\"type\":\"buypagegetdata\",\"htmldata\":\"" + productuid + "|" + openid + "\",\"resoretype\":\"" + resoretype + "\"}";
        this.postajaxs(poststr, this.showdata);
    },
    showdata: function (data) {
        if (data.result == "ok") {
            var tabledata = data.table;
            var userdata = data.userdata;
            if (tabledata.length != 0) {
                var tables = document.getElementById("tabldata");
                for (var i = 0; i < tabledata.length;i++){
                    var div = document.createElement("div");
                    div.id = tabledata[i].id;
                    div.className = "media padingset";
                    var div1 = document.createElement("div");
                    div1.className = "media-left media-middle";
                    var img = document.createElement("img");
                    img.className = "media-object img img-responsive";
                    img.src = "../" + tabledata[i].childimg;
                    div1.appendChild(img);

                    var div2 = document.createElement("div");
                    div2.className = "media-body";

                    var div3 = document.createElement("div");
                    div3.className = "media-heading pading10";
                    div3.innerHTML = "产品名称：" + $.trim(tabledata[i].product_name);

                    var div4 = document.createElement("div");
                    div4.className = "media-heading";
                    var div5 = document.createElement("div");
                    div5.id = "prices" + $.trim(tabledata[i].id);
                    div5.innerHTML = "价格：" + $.trim(tabledata[i].price);
                    var div6 = document.createElement("div");
                    div6.className = "pading10";
                    var input1 = document.createElement("input");
                    input1.className = "btn btn-md btn-default";
                    input1.id = "down" + tabledata[i].id;
                    input1.onclick = function () {
                        BuyObject.reduceproduct(this.id);
                    }
                    input1.setAttribute("type", "button");
                    input1.value = "-";

                    var input2 = document.createElement("input");
                    input2.className = "btn btn-md btn-default ";
                    input2.id = "price" + tabledata[i].id;
                    input2.name = tabledata[i].product_id;
                    input2.setAttribute("type", "button");
                    input2.value = "1";

                    var input3 = document.createElement("input");
                    input3.className = "btn btn-md btn-default";
                    input3.id = "add" + tabledata[i].id;
                    input3.onclick = function () {
                        BuyObject.addproduct(this.id);
                    }
                    input3.setAttribute("type", "button");
                    input3.value = "+";
                    var span1 = document.createElement("span");
                    span1.innerHTML = "数量：";
                    div6.appendChild(span1);
                    div6.appendChild(input1);
                    div6.appendChild(input2);
                    div6.appendChild(input3);
                    div4.appendChild(div5);
                    div4.appendChild(div6);
                    div2.appendChild(div3);
                    div2.appendChild(div4);
                    div.appendChild(div1);
                    div.appendChild(div2);
                    tables.appendChild(div);
                    BuyObject.addallprice($.trim(tabledata[i].price));
                }
            }
            if (userdata.length != 0) {
                $("#usernumber").text($.trim(userdata[0].number));
                $("#address").text("用户地址：" + $.trim(userdata[0].building + "栋" + $.trim(userdata[0].drom)));

            }
        }
        else {
            layer_alert_error(data.msg);
        }
    },
    addallprice: function (addprice) {
        var nowprice = $("#allprice").text().replace("￥", "");
        nowprice = parseInt(nowprice) + parseInt(addprice);
        $("#allprice").text("￥" + nowprice);
    },
    reduceallprice: function (addprice) {
        var nowprice = $("#allprice").text().replace("￥", "");
        nowprice = parseInt(nowprice) - parseInt(addprice);
        $("#allprice").text("￥" + nowprice);
    },
    addproduct: function (id) {
        document.getElementById("price" + id.replace("add", "")).value = parseInt(document.getElementById("price" + id.replace("add", "")).value) + 1;

        BuyObject.addallprice($("#prices" + id.replace("add", "")).text().replace("价格：", ""));
    },
    reduceproduct: function (id) {
        var nowvalue = parseInt(document.getElementById("price" + id.replace("down", "")).value) - 1;
        if (nowvalue != 0) {
            document.getElementById("price" + id.replace("down", "")).value = nowvalue;
            BuyObject.reduceallprice($("#prices" + id.replace("down", "")).text().replace("价格：", ""));
        }
    },
    tijiao:1,//判定是否已经提交
    submitOrder:function(){
        var openid = $.cookie("openid");
        if (isUndefinedNullOrEmpty(openid)) {
            alert("下单失败请稍候再购买");
            return false;
        }
        if (BuyObject.tijiao == 0) {
            alert("订单已经提交不能重复提交!");
            return;
        }
        BuyObject.tijiao = 0;
        var productuid = GetQueryString("productid");
        var amount = "";
        var count = document.getElementById("tabldata").childElementCount;
        for (var i = 0; i < count; i++) {
            if (document.getElementById("tabldata") == undefined || document.getElementById("tabldata").children[i] == undefined) {
                continue;
            }
            if (i == 0) {
                amount= document.getElementById("tabldata").children[i].getAttribute('id');
            }
            else {
                amount += "|"+document.getElementById("tabldata").children[i].getAttribute('id');
            }
            
        }

        var amountsareey = amount.split("|");
        var sendproductid = "";
        for (var f = 0; f < amountsareey.length; f++) {
            // var productids = document.getElementById("price" + amountsareey[f]).getAttribute('name');
            var productids = amountsareey[f];
            var amountss = document.getElementById("price" + amountsareey[f]).value;
            if (f == 0) {
                sendproductid = productids + "@" + amountss;
            }
            else {
                sendproductid += "|" + productids + "@" + amountss;
            }
          
            }
        
        var remaindword = $("#remaindword").val();
        var payway = this.get_radio_value();
        var data = openid + "|" + remaindword + "|" + payway + "|" + sendproductid;
        var number=$("#usernumber").text();
        var address = $("#address").text().replace("用户地址：", "");
        if (isUndefinedNullOrEmpty(number) || isUndefinedNullOrEmpty(address)) {
            alert("请填写电话号码和地址！");
            return false;
        }
        var Customerdata = openid + "|" + number + "|" + address;
        poststr = "{\"type\":\"submitOrder\",\"htmldata\":\"" + data + "\",\"customerdata\":\"" + Customerdata + "\"}";
        this.postajaxs(poststr, this.submitOrderrsult);
    
    },
    get_radio_value:function() {
        var radio = $('input[name="payway"]').filter(':checked');
        if (radio.length) {
            return radio.val();
        }
        else {
            return "no_chosie";
        }
        },
    submitOrderrsult: function (data) {
        alert(data.msg);

    },
    saveusermasage: function () {
        var number = $("#tel1").val();
        var address = $("#dnumber").val();
        var build = $("#build").val();
        if (isUndefinedNullOrEmpty(number) || isUndefinedNullOrEmpty(address)||isUndefinedNullOrEmpty(build)) {
            alert("电话号码和地址都不能为空！");
            return false;
        }
        $("#usernumber").text(number);
        $("#address").text("用户地址：" + build+"栋"+address);
        $('#myTable').modal('hide')
    },
    buyproduct: function () {
        var productuid = GetQueryString("productid");
        location.href = "buy.aspx?" + "productid=" + productuid + "&r=" + Math.random();
    },
    postajaxs: function (postdata, fangfa) {
        mj_ajax("../Handler1.ashx", "json", postdata, fangfa)
    }
}